#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
/*void sort(int arr[], int se)    //循环方式实现
{
	int x = 0, y = 0;
	int k = 0;   //中间变量
	int count = 0;
	int flag = 0;
	for (x = se-1;x > 1; x--)   //外层指要排的次数
	{
		flag =0;
		for (y = 0; y < x; y++)   //内层进行排序
		{
			if (arr[y] > arr[y + 1])
			{
				k = arr[y + 1];
				arr[y + 1] = arr[y];
				arr[y] = k;
				flag =1;
			}
		}
		count++;
		printf("count:%d ", count);
		if(flag ==0) break;
	}
}*/
void sort(int arr[], int se)     //递归实现
{
	if (se < 2) return 1;
	int y = 0, k = 0;
	int flag = 1;
	static int count = 0;
	for (y = 0; y < se-1; y++)
	{
		if (arr[y] > arr[y + 1])      //排序
		{
			k = arr[y + 1];
			arr[y + 1] = arr[y];
			arr[y] = k;
			flag = 0;
		}
	}
	count++;
	printf("count:%d ", count);
	if (flag) return 1;   //优化排序，已排就不用再排
	return sort(arr, --se);
}

int main()
{
	int arr[] = { 2,4,22,44,3,8,51,67,21,76 };
	int se = sizeof(arr) / sizeof(arr[0]);
	sort(arr, se);
	for (int j = 0; j < se; j++)
	{
		printf("%d ", arr[j]);
	}
	return 0;
}